#!/usr/bin/env python3
"""
修复密码验证问题
"""

import sys
import os
import sqlite3

# 添加路径
sys.path.append(os.path.join(os.path.dirname(__file__), 'app'))

from app.routes.auth import get_password_hash, verify_password

def reset_password():
    """重置用户密码"""
    print("=== 重置用户密码 ===")
    
    conn = sqlite3.connect('evintro.db')
    cursor = conn.cursor()
    
    # 获取当前密码哈希
    cursor.execute('SELECT username, password_hash FROM users WHERE username = ?', ('guard1',))
    result = cursor.fetchone()
    
    if result:
        username, old_hash = result
        print(f"当前密码哈希: {old_hash}")
        
        # 生成新的密码哈希
        new_password = "123456"
        new_hash = get_password_hash(new_password)
        print(f"新密码哈希: {new_hash}")
        
        # 验证新哈希
        is_valid = verify_password(new_password, new_hash)
        print(f"新密码验证: {is_valid}")
        
        # 更新数据库
        cursor.execute('UPDATE users SET password_hash = ? WHERE username = ?', (new_hash, username))
        conn.commit()
        print("密码已更新")
    
    # 对所有用户执行相同操作
    cursor.execute('SELECT id, username, password_hash FROM users')
    users = cursor.fetchall()
    
    for user_id, username, old_hash in users:
        new_hash = get_password_hash("123456")
        cursor.execute('UPDATE users SET password_hash = ? WHERE id = ?', (new_hash, user_id))
        print(f"更新用户 {username} 的密码")
    
    conn.commit()
    conn.close()
    print("所有用户密码已重置为 123456")

if __name__ == "__main__":
    reset_password()